let wxCharts = require("../../mars/plugins/charts.js");
let api = require("../../mars/services/api.js");
let that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    punchData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getPunchRecords();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  getPunchRecords() {
    let user_id = wx.getStorageSync('user').id;
    api.WxRequest(api.getPunchRecords, {
      userId: user_id
    }, function(res) {
      if (!res.data.length) return;
      if (res.data.length > 7) {
        res.data = res.data.slice(res.data.length - 7);
      }
      let xs = res.data.map(item => {
        return item.created_time;
      });
      let ys = res.data.map(item => {
        return item.weight;
      })
      xs = xs.reverse();
      ys = ys.reverse();
      that.setData({
        punchData: res.data
      });
      that.initChart(xs, ys);
    });
  },
  initChart(xs, ys) {
    new wxCharts({
      canvasId: 'lineCanvas',
      type: 'line',
      categories: xs,
      series: [{
        name: '日期',
        data: ys,
        format: function(val) {
          return val.toFixed(0);
        }
      }],
      yAxis: {
        title: '体重',
        format: function(val) {
          return val.toFixed(0);
        },
        min: 0
      },
      width: 320,
      height: 200
    });
  }
})